<template>
  <div>
    <!-- 产品管理弹窗 -->
    <t-dialog :visible.sync="visible" :width="680" :footer="false" :closeBtn="false">
      <div slot="header">{{ title }}</div>
      <div slot="body">
        <t-divider :style="{ marginTop: '1px' }"></t-divider>
        <!-- 表单内容 -->
        <t-form :data="formDataAddHost" ref="form" :rules="rules" @submit="commit" :labelWidth="100">
          <!-- 表单项 -->
          <t-form-item label="主机名称" name="host_name">
            <t-input :style="{ width: '480px' }" v-model="formDataAddHost.host_name" placeholder="请输入主机名称"></t-input>
          </t-form-item>
          <t-form-item label="内网or外网" name="inside_or_outside">
            <t-radio-group v-model="formDataAddHost.inside_or_outside">
              <t-radio :value=0>内网</t-radio>
              <t-radio :value=1>外网</t-radio>
            </t-radio-group>
          </t-form-item>
          <t-form-item label="内网ip" name="inside_ipaddr">
            <t-input :style="{ width: '480px' }" v-model="formDataAddHost.inside_ipaddr" placeholder="请输入内网ip地址"></t-input>
          </t-form-item>
          <t-form-item label="外网ip" name="outside_ipaddr">
            <t-input :style="{ width: '480px' }" v-model="formDataAddHost.outside_ipaddr" placeholder="请输入外网ip地址"></t-input>
          </t-form-item>
          <t-form-item label="端口号" name="ssh_port">
            <t-input :style="{ width: '480px' }" v-model="formDataAddHost.ssh_port" placeholder="请输入端口号"></t-input>
          </t-form-item>
          <t-form-item label="用户名" name="username">
            <t-input :style="{ width: '480px' }" v-model="formDataAddHost.username" placeholder="请输入用户名"></t-input>
          </t-form-item>
          <t-form-item label="密码" name="password">
            <t-input type="password" :style="{ width: '480px' }" v-model="formDataAddHost.password" placeholder="请输入密码"></t-input>
          </t-form-item>
          <t-form-item style="float: right">
            <t-button variant="outline" @click="onCancel" :style="{ marginRight: '10px' }">取消</t-button>
            <t-button theme="primary" type="submit">确定</t-button>
          </t-form-item>
        </t-form>
      </div>
    </t-dialog>
  </div>
</template>

<script>
export default {
  name: 'HostDialog',
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    formData: {
      type: Object,
      default: () => ({}),
    },
    title: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      rules: {
        // 表单验证规则
      },
      visible: this.visible,
      formDataAddHost: { ...this.formData }, // 将父组件传递的数据赋值给formDataAddHost
    };
  },
  watch: {
    visible(newVal) {
      this.visible = newVal;
    },
    formData(newVal) {
      this.formDataAddHost = { ...newVal }; // 监听父组件传递的数据变化，并更新formDataAddHost
    },
  },
  mounted() {
    this.visible = this.visible;
    this.formDataAddHost = { ...this.formData };
    },
  methods: {
    // commit() {
    //   // 处理弹窗提交事件
    //   this.$emit('submit', this.formDataAddHost); // 触发 submit 事件并传递 formDataAddHost 数据
    //   console.log("我拿到提交信息了吗的信息了吗",this.formDataAddHost)
    // },
    commit() {
      let params = {};
      if (this.formDataAddHost.id) {
        // 存在 id 字段，表示修改数据
        const opType = '2'; // 修改操作的类型
        params = {
          "ids": [this.formDataAddHost.id],
          param: {
            id: this.formDataAddHost.id,
            host_name: this.formDataAddHost.host_name,
            inside_ipaddr: this.formDataAddHost.inside_ipaddr,
            outside_ipaddr: this.formDataAddHost.outside_ipaddr,
            inside_or_outside: this.formDataAddHost.inside_or_outside,
            username: this.formDataAddHost.username,
            password: this.formDataAddHost.password,
            ssh_port: this.formDataAddHost.ssh_port,
          },
          op_type: opType
        };
      } else {
        // 不存在 id 字段，表示注册新数据
        const opType = '1'; // 创建操作的类型
        params = {
          "ids": [0],
          param: {
            id: 0,
            host_name: this.formDataAddHost.host_name,
            inside_ipaddr: this.formDataAddHost.inside_ipaddr,
            outside_ipaddr: this.formDataAddHost.outside_ipaddr,
            inside_or_outside: this.formDataAddHost.inside_or_outside,
            username: this.formDataAddHost.username,
            password: this.formDataAddHost.password,
            ssh_port: this.formDataAddHost.ssh_port,
          },
          op_type: opType
        };
      }

      console.log('提交的信息:', params);

      this.$request
        .post('/api/operate/hosts', params)
        .then((res) => {
          this.$message.success('提交成功');
          // 关闭弹窗
          this.$emit('cancel');
        })
        .catch((e) => {
          console.log(e);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    onCancel() {
      // 处理弹窗取消事件
      this.$emit('cancel'); // 触发 cancel 事件
    },
  },
};
</script>

<style scoped>
/* 样式内容 */
</style>
